<?php
/*
 * 订单相关的请求皇马支付的逻辑
 */
namespace Mobile\Controller;

use Mobile\Model\orderModel;
class OrderhmController extends BaseController {
    
    public $hm;
    public $orderModel;
    public $payModel;
    public function __construct() {
        parent::__construct();
        $this->hm = new PayhmController();
        $this->orderModel = new orderModel();
        $this->payModel = M('pay');
    }
    
    
    
    public function qrcode(){
        
        $order_sn = get("order_sn");
        $type = get('type');
        if(!$order_sn){
            exit("order_sn error");
        }
        $pay_url = $this->_getPayUrl($order_sn, $type);
        qrcode($pay_url);
    
    }
    

    public function checkorder(){
        $order_sn = get('order_sn');
        $where = array(
            "order_sn" => $order_sn,
        );
        $payorder = $this->payModel->where($where)->find();
        $order = $this->orderModel->where($where)->find();
        //检查是否使用微信支付
        $res = $this->hm->wechatCheckOrder($payorder['out_trade_no']);
        if($res){
            if($order['checked'] == 0){
                $orderController = new OrdercallbackController();
                $orderController->_changeOrder($order, 5);
                //$this->_changeOrder($order, 5);
                return_json(0,"支付成功",'','/mobile/orderdetail');
            }
        }else{
    
            //检查是否使用支付宝支付TODO
            return_json(1,"支付失败",'','/mobile/orderdetail');
        }
    }
    
    public function callback(){
        $arr = $_POST;
        if($arr['return_code'] == 10000 && $arr['trade_result'] == 'success'){
            $hm = new PayhmController();
            $sign = $hm->getCallbackSign($arr);
            if($sign == $arr['sign']){
                $where = array('order_sn'=>$arr['pay_num']);
                $order = $this->orderModel->where($where)->find();
                if($order['checked'] == 0){
                    $orderController = new OrdercallbackController();
                    $orderController->_changeOrder($order, 5);
                    echo 'ok';
                }
            }else{
                file_put_contents('11.txt', 'sign验证失败');
            }
        }
    }
    
    /*
     * 网银回调
     * trade_result==1成功 2失败
     */
    public function wangyincallback(){
        $arr = $_POST;
        if($arr['return_code'] == 10000 && $arr['trade_result'] == 1){
            $hm = new PayhmController();
            $sign = $hm->getCallbackSign($arr,true);
            if($sign == $arr['sign']){
                $where = array('order_sn'=>$arr['pay_num']);
                $order = $this->orderModel->where($where)->find();
                if($order['checked'] == 0){
                    $orderController = new OrdercallbackController();
                    $orderController->_changeOrder($order, 7);
                    echo 'success';
                }
            }else{
                file_put_contents('11.txt', 'sign验证失败');
            }
        }
    }
    
    protected function _getPayUrl($order_sn,$type){
    
        $orderWhere = array(
            "order_sn" => $order_sn,
        );
        $payOrderWhere = array(
            "order_sn" => $order_sn,
            'pay_type' => $type
        );
    
        $order = $this->orderModel->getOrder($orderWhere);
        
    
        if($order['checked'] != 0){
            return false;
        }
    
        //若订单存在切未过期 直接返回url
        $payorder = $this->payModel->where($payOrderWhere)->find();
    
        if($payorder){
    
            if($payorder['expired_time']>date("Y-m-d H:i:s") && $payorder['code_url']!=null){
                return $payorder['code_url'];
            }
        }
    
        
        if($type == 5){
            $notifyurl = get_domain().'/mobile/orderhm/callback/order_sn/'.$payorder['order_sn'];
            $res = $this->hm->wechatCreateOrder($order,$notifyurl);
        }elseif($type == 3){
            $res = $this->hm->alipayCreateOrder($order);
        }
    
    
        /*  $data = array(
         "out_trade_no" => $out_trade_no,//返回的外部订单号
         "code_url" => $code_url,//二维码内容
         ); */
        $data = array(
            'uid' => $order['uid'],
            'code_url' => $res['code_url'],
            'out_trade_no' => $res['out_trade_no'],
            'order_sn' => $order['order_sn'],
            'order_id' => $order['order_id'],
            'pay_type' => 5,
            'create_time' => date("Y-m-d H:i:s"),
            'expired_time' => date("Y-m-d H:i:s",strtotime('+3600 second')),
        );
    
        if($payorder){
            $this->payModel->where($payOrderWhere)->save($data);
        }else{
            $this->payModel->add($data);
        }
        //$this->orderModel->where($where)->setField('transaction_id',$res['out_trade_no']);
        return $res['code_url'];
    }
    
    
    public function testcallback() {
        file_put_contents('orderhm.txt', json_encode($_POST));
    }
    

}

